package com.ruoyi.project.poem.goodsbanner.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.poem.goodsbanner.domain.GoodsBanner;
import com.ruoyi.project.poem.goodsbanner.service.IGoodsBannerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 首页bannerController
 *
 * @author shixiangwen
 * @date 2021-06-24
 */
@Controller
@RequestMapping("/poem/goodsbanner")
public class GoodsBannerController extends BaseController {
    private String prefix = "poem/goodsbanner";

    @Autowired
    private IGoodsBannerService goodsBannerService;

    @RequiresPermissions("poem:goodsbanner:view")
    @GetMapping()
    public String goodsbanner() {
        return prefix + "/goodsbanner";
    }

    /**
     * 查询首页banner列表
     */
    @RequiresPermissions("poem:goodsbanner:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GoodsBanner goodsBanner) {
        startPage();
        List<GoodsBanner> list = goodsBannerService.selectGoodsBannerList(goodsBanner);
        return getDataTable(list);
    }

    /**
     * 导出首页banner列表
     */
    @RequiresPermissions("poem:goodsbanner:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GoodsBanner goodsBanner) {
        List<GoodsBanner> list = goodsBannerService.selectGoodsBannerList(goodsBanner);
        ExcelUtil<GoodsBanner> util = new ExcelUtil<GoodsBanner>(GoodsBanner.class);
        return util.exportExcel(list, "goodsbanner");
    }

    /**
     * 新增首页banner
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存首页banner
     */
    @RequiresPermissions("poem:goodsbanner:add")
    @Log(title = "首页banner", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GoodsBanner goodsBanner) {
        return toAjax(goodsBannerService.insertGoodsBanner(goodsBanner));
    }

    /**
     * 修改首页banner
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        GoodsBanner goodsBanner = goodsBannerService.selectGoodsBannerById(id);
        mmap.put("goodsBanner", goodsBanner);
        return prefix + "/edit";
    }

    /**
     * 修改保存首页banner
     */
    @RequiresPermissions("poem:goodsbanner:edit")
    @Log(title = "首页banner", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GoodsBanner goodsBanner) {
        return toAjax(goodsBannerService.updateGoodsBanner(goodsBanner));
    }

    /**
     * 删除首页banner
     */
    @RequiresPermissions("poem:goodsbanner:remove")
    @Log(title = "首页banner", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(goodsBannerService.deleteGoodsBannerByIds(ids));
    }

    @RequiresPermissions("poem:goodsbanner:edit")
    @Log(title = "首页banner", businessType = BusinessType.GRANT)
    @PostMapping("/updateStatus/{id}")
    @ResponseBody
    public AjaxResult updateStatus(@PathVariable("id") Long id, Integer status) {

        GoodsBanner goodsBanner = goodsBannerService.selectGoodsBannerById(id);
        goodsBanner.setStatus(status);

        return toAjax(goodsBannerService.updateGoodsBanner(goodsBanner));
    }
}
